<template>
  <div>
    <page-dialog
      :title="dialogInfo.title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <!-- 条件栏 -->
      <page-filter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
      />
      <dialog-table
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        ref="depotTable"
        v-model:data="tableInfo.data"
        :api="api"
        :listen-height="false"
        v-model:query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :highlight-row="highlightRow"
        :check-box="checkBox"
        :reserve-selection="true"
        :checked-row-list="checkedRowList"
        :checkedList="checkBox ? checkedList : []"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-lock="scope">
          {{ scope.row.lock ? '盘点中' : '正常' }}
        </template>
      </dialog-table>
    </page-dialog>
  </div>
</template>

<script setup name="selectDepot">
import PageDialog from '../PageDialog/index'
import PageFilter from '../PageFilter/index'
import { getDepotPage } from '@/api/core.js'
import DialogTable from '../DialogTable/index'
const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getDepotPage,
  },
  showDialog: {
    type: Boolean,
  },
  isLock: {
    type: Boolean,
    default: true,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  defaultList: {
    type: Array,
    default: function () {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
  checkedRowList: {
    type: Array,
    default: function () {
      return []
    },
  },
  checkedList: {
    type: Array,
    default: function () {
      return []
    },
  },
  fieldList: {
    type: Array,
    default: function () {
      return [
        { label: '名称', value: 'name' },
        { label: '编码', value: 'code' },
        { label: '状态', value: 'lock', type: 'slot' },
      ]
    },
  },
  filterInfoList: {
    type: Array,
    default: function () {
      return []
    },
  },
  // checkLock: { //是否校验盘点状态
  //   type: Boolean,
  //   default: false
  // }
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    sort: 'create_time desc',
    ...props.query,
    searchType: 'depot',
  },
  list: [
    {
      type: 'input',
      label: '名称/编码',
      value: 'keyword',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: props.fieldList,
})
// 弹窗相关
const dialogInfo = ref({
  title: '选择仓库',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')
const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})
watch(
  () => props.showDialog,
  (val) => {
    if (val) {
      proxy.$nextTick(() => {
        getList()
      })
    }
  }
)
function fullQuery() {
  if (props.filterInfoList.length) {
    let arr = [{ type: 'button', label: '搜索' }]
    filterInfo.value.list = props.filterInfoList.concat(arr)
    filterInfo.value.list.forEach((item) => {
      item.event = search
    })
  }
}
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  const _tableInfo = tableInfo.value
  _tableInfo.initCurpage = Math.random()
  _tableInfo.refresh = Math.random()
}
function save() {
  // emits('input', data)
  if (selectRow.value) {
    if (props.isLock) {
      if (selectRow.value.lock) {
        proxy.$message({
          type: 'warning',
          duration: 3500,
          showClose: true,
          message: selectRow.value.name + '已经被锁定，请等待解锁',
        })
        return
      }
    }
    handleClose()
    emits('callback', selectRow.value)
  } else {
    handleClose()
    emits('callback', selectRow.value)
    // proxy.$message({
    //   type: "warning",
    //   duration: 3500,
    //   showClose: true,
    //   message: "请选择仓库"
    // });
  }
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      if (!proxy.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
    case 'tableCheck':
      selectRow.value = data
      if (!proxy.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    getList()
    fullQuery()
    if (props.defaultList.length) {
      selectRow.value = props.defaultList
      props.defaultList.forEach((item) => {
        proxy.$refs['depotTable'].toggleRowSelection(item)
      })
    }
  })
})
</script>

<style scoped lang="scss"></style>
